package com.wc.codeforces.前缀和与差分.前缀和.Beauty_of_the_mountains;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/6/25 23:56
 * @description https://codeforces.com/contest/1982/problem/D
 */
public class Main {
    /**
     * 思路：
     * 前缀和求一个区间内的有帽子和没有帽子的差别
     * 计算总的差值
     * 然后寻找这个差值是否可以是整除以 gcd(差值)
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 510;
    static int[][] h = new int[N][N];
    static char[][] hat = new char[N][];
    static int[][] sum = new int[N][N];
    static int[] re = new int[N * N];
    static int n, m, k;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            k = sc.nextInt();
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    h[i][j] = sc.nextInt();
                }
            }
            long hats = 0, nohat = 0;
            for (int i = 1; i <= n; i++) {
                hat[i] = (" " + sc.next()).toCharArray();
                for (int j = 1; j <= m; j++) {
                    sum[i][j] = 0;
                    sum[i][j] += hat[i][j] - '0' + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
                    if (hat[i][j] == '0') nohat += h[i][j];
                    else hats += h[i][j];
                }
            }
            long sub = hats - nohat;
            int len = 0;
            for (int i = k; i <= n; i++) {
                for (int j = k; j <= m; j++) {
                    int hatNum = sum[i][j] - sum[i][j - k] - sum[i - k][j] + sum[i - k][j - k];
                    int rep = hatNum - (k * k - hatNum);
                    re[++len] = Math.abs(rep);
                }
            }
            int gcd = re[1];
            for (int i = 1; i <= len; i++) gcd = gcd(gcd, re[i]);
            // 一定需要操作一次
            if (gcd == 0 && sub == 0 || gcd > 0 && sub % gcd == 0) out.println("YES");
            else out.println("NO");
        }
        out.flush();
    }

    static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
